package edu.langnan.xiaoli.controller.business;

import edu.langnan.xiaoli.base.AjaxResult;
import edu.langnan.xiaoli.base.controller.BaseController;
import edu.langnan.xiaoli.config.security.TokenService;
import edu.langnan.xiaoli.core.dto.MomentDTO;
import edu.langnan.xiaoli.service.MomentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author cql
 */
@RestController
@RequestMapping("moment")
public class MomentController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private MomentService momentService;


    /**
     * 发布说说
     *
     * @param momentDTO
     * @param request
     * @return
     */
    @PostMapping
    public AjaxResult createMoment(@RequestBody MomentDTO momentDTO, HttpServletRequest request) {

        Integer stuId = tokenService.getLoginUser(request).getId();
        momentDTO.setStuId(stuId);
        momentDTO.setCreateTime(new Date());

        momentService.createMoment(momentDTO);

        return AjaxResult.success();
    }

    /**
     * 删除说说
     *
     * @param momentId
     * @param request
     * @return
     */
    @DeleteMapping("{momentId}")
    public AjaxResult deleteMoment(@PathVariable("momentId") Integer momentId, HttpServletRequest request) {
        Integer stuId = tokenService.getLoginUser(request).getId();
        momentService.deleteMoment(stuId, momentId);
        return AjaxResult.success();
    }


}
